package com.fpt.edu.vn.h2tv.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.fpt.edu.vn.h2tv.common.ElementName;
import com.fpt.edu.vn.h2tv.dao.EntityManagerHelper;
import com.fpt.edu.vn.h2tv.dao.ITbBookDAO;
import com.fpt.edu.vn.h2tv.dao.TbBookCategoryDAO;
import com.fpt.edu.vn.h2tv.dao.TbBookDAO;
import com.fpt.edu.vn.h2tv.dao.TbCategoryDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbBook;
import com.fpt.edu.vn.h2tv.dao.entity.TbBookCategory;
import com.fpt.edu.vn.h2tv.dao.entity.TbBookCategoryId;
import com.fpt.edu.vn.h2tv.dto.Book;
import com.fpt.edu.vn.h2tv.dto.BookResponse;

public class BookStoreService implements IBaseService {

    @Override
    public Object getResponse() {

        return getBooksResponse();
    }

    private BookResponse getBooksResponse() {
        ITbBookDAO bookDAO = new TbBookDAO();
        // find all books on DB
        List<TbBook> tbBookList = bookDAO.findAll();
        BookResponse response = new BookResponse();
        List<Book> bookList = new ArrayList<Book>();

        if (tbBookList != null && tbBookList.size() > 0) {
            for (TbBook book : tbBookList) {
                Book bookDTO = new Book();
                bookDTO.setBookId(book.getBooktid().intValue());
                bookDTO.setBookName(book.getBookname());
                bookDTO.setCategoryId(book.getCategoryid().shortValue());
                bookDTO.setAuthor(book.getAuthor());
                bookDTO.setDescription(book.getDescription());
                bookDTO.setPrice(book.getPrice().doubleValue());
                bookDTO.setQuantity(book.getQuantity().intValue());
                bookDTO.setCount(book.getCount().longValue());
                bookDTO.setPicture(book.getBookImagel());
                bookList.add(bookDTO);
            }
            response.setStatus(ElementName.SUCCESS);

        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setBooks(bookList);
        return response;
    }

    public static String updateBook(int id, String name, Short categoryId, String author, Double price, String des,
            int quantity, String imgUrl) {
        String success;
        ITbBookDAO bookDAO = new TbBookDAO();
        TbCategoryDAO cateDao = new TbCategoryDAO();        
        TbBookCategoryDAO bcDao = new TbBookCategoryDAO();
        TbBookCategory category = new TbBookCategory();
        TbBook book = bookDAO.findById(id);
        if (book != null) {
            book.setBookname(name);
            book.setCategoryid(categoryId);
            book.setAuthor(author);
            book.setPrice(price);
            book.setDescription(des);
            book.setQuantity(quantity);
            if(imgUrl!=null && !imgUrl.equals("")){
                book.setBookImagel(imgUrl);
            }
            

            // Transaction
            EntityManagerHelper.beginTransaction();
            TbBook result = bookDAO.update(book);
            EntityManagerHelper.commit();
            
            category.setId(new TbBookCategoryId(categoryId, result.getBooktid()));
            category.setTbBook(result);
            category.setTbCategory(cateDao.findById(categoryId));

            EntityManagerHelper.beginTransaction();
            bcDao.update(category);
            EntityManagerHelper.commit();
            
            success = "update bookID " + id + " successful!";
        } else {
            success = "update bookID " + id + " failed!";
        }

        return success;
    }

    public static String deleteBook(int id) {
        ITbBookDAO bookDAO = new TbBookDAO();
        TbBook book = bookDAO.findById(id);
        String success;
        if (book != null) {
            // Transaction
            EntityManagerHelper.beginTransaction();
            bookDAO.delete(book);
            EntityManagerHelper.commit();
            success = "delete bookID " + id + " successful!";
        } else {
            success = "delete bookID " + id + " failed!";
        }
        return success;
    }

    public static String addBook(String name, Short categoryId, String author, Double price, String des, String imgUrl,
            Timestamp CreateTime, Long count, int quantity, Timestamp updateTime) {
        ITbBookDAO bookDAO = new TbBookDAO();
        TbCategoryDAO cateDao = new TbCategoryDAO();
        TbBook book = new TbBook();
        TbBookCategoryDAO bcDao = new TbBookCategoryDAO();
        TbBookCategory category = new TbBookCategory();

        book.setBookname(name);
        book.setCategoryid(categoryId);
        book.setAuthor(author);
        book.setPrice(price);
        book.setDescription(des);
        book.setCreatetime(CreateTime);
        book.setQuantity(quantity);
        book.setCount(count);
        book.setUpdatetime(updateTime);
        if(imgUrl!=null && !imgUrl.equals("")){
            book.setBookImagel(imgUrl);
        } else{
            book.setBookImagel("default");
        }
        

        // Transaction
        EntityManagerHelper.beginTransaction();
        TbBook result = bookDAO.update(book);
        EntityManagerHelper.commit();

        category.setId(new TbBookCategoryId(categoryId, result.getBooktid()));
        category.setTbBook(result);
        category.setTbCategory(cateDao.findById(categoryId));

        EntityManagerHelper.beginTransaction();
        bcDao.save(category);
        EntityManagerHelper.commit();

        String success = "add bookName " + name + " successful!";
        return success;
    }

    public static BookResponse searchBooks(String key) {
        ITbBookDAO bookDAO = new TbBookDAO();

        List<TbBook> tbBookList = bookDAO.findBookByKeyword(key);
        BookResponse response = new BookResponse();
        List<Book> bookList = new ArrayList<Book>();

        if (tbBookList != null && tbBookList.size() > 0) {
            for (TbBook book : tbBookList) {
                Book bookDTO = new Book();
                bookDTO.setBookId(book.getBooktid().intValue());
                bookDTO.setBookName(book.getBookname());
                bookDTO.setCategoryId(book.getCategoryid().shortValue());
                bookDTO.setAuthor(book.getAuthor());
                bookDTO.setDescription(book.getDescription());
                bookDTO.setPrice(book.getPrice().doubleValue());
                bookDTO.setQuantity(book.getQuantity().intValue());
                bookDTO.setCount(book.getCount().longValue());
                bookDTO.setPicture(book.getBookImagel());
                bookList.add(bookDTO);
            }
            response.setStatus(ElementName.SUCCESS);

        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setBooks(bookList);
        return response;
    }
    
    public static BookResponse searchHotBook() {
        ITbBookDAO bookDAO = new TbBookDAO();
        // find all books on DB
        List<TbBook> tbBookList = bookDAO.sortBookByCount();
        BookResponse response = new BookResponse();
        List<Book> bookList = new ArrayList<Book>();

        if (tbBookList != null && tbBookList.size() > 0) {
            for (TbBook book : tbBookList) {
                Book bookDTO = new Book();
                bookDTO.setBookId(book.getBooktid().intValue());
                bookDTO.setBookName(book.getBookname());
                bookDTO.setCategoryId(book.getCategoryid().shortValue());
                bookDTO.setAuthor(book.getAuthor());
                bookDTO.setDescription(book.getDescription());
                bookDTO.setPrice(book.getPrice().doubleValue());
                bookDTO.setQuantity(book.getQuantity().intValue());
                bookDTO.setCount(book.getCount().longValue());
                bookDTO.setPicture(book.getBookImagel());
                bookList.add(bookDTO);
            }
            response.setStatus(ElementName.SUCCESS);

        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setBooks(bookList);
        return response;
    }
    
    public static BookResponse searchOutOfBook() {
        ITbBookDAO bookDAO = new TbBookDAO();
        // find all books on DB
        List<TbBook> tbBookList = bookDAO.searchOutOfBook();
        BookResponse response = new BookResponse();
        List<Book> bookList = new ArrayList<Book>();

        if (tbBookList != null && tbBookList.size() > 0) {
            for (TbBook book : tbBookList) {
                Book bookDTO = new Book();
                bookDTO.setBookId(book.getBooktid().intValue());
                bookDTO.setBookName(book.getBookname());
                bookDTO.setCategoryId(book.getCategoryid().shortValue());
                bookDTO.setAuthor(book.getAuthor());
                bookDTO.setDescription(book.getDescription());
                bookDTO.setPrice(book.getPrice().doubleValue());
                bookDTO.setQuantity(book.getQuantity().intValue());
                bookDTO.setCount(book.getCount().longValue());
                bookDTO.setPicture(book.getBookImagel());
                bookList.add(bookDTO);
            }
            response.setStatus(ElementName.SUCCESS);

        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setBooks(bookList);
        return response;
    }

}
